/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player.abilities;

import static com.cmp.core.constants.InputUIDConstants.ABILITY_BUTTON_UID;
import static com.cmp.core.constants.InputUIDConstants.CURSOR_POS_UID;
import static com.cmp.core.constants.UIDConstants.BALL_UID;
import com.cmp.core.utilities.PhysicsUtility;
import com.cmp.player.controls.CharacterRigidBodyControl;
import com.cmp.player.controls.CursorSelectionControl;
import com.cmp.player.controls.MoveControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 * Fetcher ability.
 * @author krummer
 */
public class PushControl extends AbilityControl {
    
    private static final float PUSH_SPEED = 23.f;
    private static final float PUSH_DISTANCE = 10.f;
    private boolean isAbility;
    private CharacterRigidBodyControl body;
    
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        super.setCooldown(3.f);
        super.setCastDuration(0.5f);
        
        spatial.setUserData(ABILITY_BUTTON_UID, false);
        this.body = spatial.getControl(CharacterRigidBodyControl.class);
    }
    
    @Override
    public void execute() {
        //only do it without the ball
        Node ball = (Node) ((Node)this.spatial).getChild(BALL_UID);
        
        //only do when selecting a player.
        Node selected = this.getSpatial().getControl(CursorSelectionControl.class).getSelection();
        
        
        if((ball == null && selected != null 
        && !(this.isOnCooldown())) && body.isOnGround() 
        && ((selected.getUserData("isPlayer")) != null)) {
      
            super.activateCooldown();
            super.activateGCD();
            //stop moving before casting.
            this.body.setLinearVelocity(Vector3f.ZERO);
            super.activateCastTime();
            
          
            //throw enemy towards you! WEEEEEEEEEEEEEEEE!
            if(super.getSpatial().getUserData(CURSOR_POS_UID) != null) {
                Vector3f targetPos = ((Vector3f)super.getSpatial().getUserData(CURSOR_POS_UID)).clone();
                Vector3f myPos = getSpatial().getWorldTranslation().clone();
                    
                // push enemy away
                Vector3f pushDir = targetPos.subtract(myPos);
                pushDir = pushDir.add(Vector3f.UNIT_Y);
                pushDir = pushDir.normalize();
                    
                Vector3f pushPos;
                pushPos = targetPos.clone();
                pushPos = pushPos.add(pushDir.mult(PUSH_DISTANCE));
                    
                // throw enemy at mouse pos.
                Vector3f throwArch = PhysicsUtility
                        .calculateVerticalLaunchSpeed(body.getGravity().y, PUSH_SPEED, targetPos, pushPos);
                selected.getControl(CharacterRigidBodyControl.class)
                        .setLinearVelocity(throwArch);
                    
                //lock enemies movement for duration of cast.
                selected.getControl(MoveControl.class)
                        .setRooted(true, this.getCastDuration());
                    
                selected.getControl(CursorSelectionControl.class)
                        .setRooted(true, this.getCastDuration());
            }
        }
    }

    @Override
    protected void controlUpdate(float tpf) {
        super.controlUpdate(tpf);
        
        isAbility = super.getSpatial().getUserData(ABILITY_BUTTON_UID);
        if(isAbility) {
            execute();
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
